package com.cat.dataStructure;

import java.util.HashMap;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/count-nice-pairs-in-an-array/description/
 * @create 2025/8/14 16:31
 * @since JDK17
 */

public class Solution10 {
    static int N = ((int) (1e9 + 7));
    int reverse(int n) {
        int x = 0;
        while (n != 0) {
            x = n % 10 + x * 10;
            n /= 10;
        }
        return x;
    }
    static HashMap<Integer, Integer> map = new HashMap<>();
    public int countNicePairs(int[] nums) {
        int n = nums.length;
        int ans = 0, cnt;
        map.clear();
        for (int i  = 0, t; i < n; i++) {
            t = nums[i] - reverse(nums[i]);
            cnt = map.getOrDefault(t, 0);
            ans = (ans + cnt) % N;
            map.put(t, cnt + 1);
        }
        return ans;
    }
}
